
package com.fx.apartment.modules.activity.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fx.apartment.common.support.Condition;
import com.fx.apartment.common.support.Query;
import com.fx.apartment.common.tool.api.R;
import com.fx.apartment.common.tool.utils.Func;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fx.apartment.modules.activity.entity.Activity;
import com.fx.apartment.modules.activity.vo.ActivityVO;
import com.fx.apartment.modules.activity.wrapper.ActivityWrapper;
import com.fx.apartment.modules.activity.service.IActivityService;

import java.util.List;

/**
 * 活动设置 控制器
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/activity")
@Api(value = "活动设置", tags = "活动设置接口")
public class ActivityController {

	private final IActivityService activityService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入activity")
	public R<ActivityVO> detail(Activity activity) {
		Activity detail =null;
		if(Func.isEmpty(activity.getId())){
			List<Activity> list = activityService.list(Wrappers.<Activity>lambdaQuery().orderByDesc(Activity::getEndTime));
			if(Func.isEmpty(list)){
				return R.fail("暂时没有活动");
			}
			detail = list.get(0);
		}else {
			detail = activityService.getById(activity);
		}
		return R.data(ActivityWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 活动设置
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入activity")
	public R<IPage<ActivityVO>> list(Activity activity, Query query) {
		IPage<Activity> pages = activityService.page(Condition.getPage(query), Condition.getQueryWrapper(activity).orderByDesc("end_time"));
		return R.data(ActivityWrapper.build().pageVO(pages));
	}

	/**
	 * 新增或修改 活动设置
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入activity")
	public R submit(@Valid @RequestBody Activity activity) {
		return R.status(activityService.saveOrUpdate(activity));
	}


	/**
	 * 删除 活动设置
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(activityService.deleteLogic(Func.toLongList(ids)));
	}


}
